﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RemoveFunction
{
    public partial class Form2 : Form
    {
        public Form2()
        {
                InitializeComponent();
            button1_Click(null, null);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                textBox1.Text = SqliteoneQuery("select ConfigValue From ConfigSetting where ConfigKey='DataBase'");
                textBox2.Text = SqliteoneQuery("select ConfigValue From ConfigSetting where ConfigKey='ftpServerIP'");
                textBox3.Text = SqliteoneQuery("select ConfigValue From ConfigSetting where ConfigKey='ftpUserID'");
                textBox4.Text = SqliteoneQuery("select ConfigValue From ConfigSetting where ConfigKey='ftpPassword'");
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString()); }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                SqliteoneQuery($"Update ConfigSetting set ConfigValue='{textBox1.Text}' where ConfigKey='DataBase'");
                SqliteoneQuery($"Update ConfigSetting set ConfigValue='{textBox2.Text}' where ConfigKey='ftpServerIP'");
                SqliteoneQuery($"Update ConfigSetting set ConfigValue='{textBox3.Text}' where ConfigKey='ftpUserID'");
                SqliteoneQuery($"Update ConfigSetting set ConfigValue='{textBox4.Text}' where ConfigKey='ftpPassword'");
                MessageBox.Show("保存成功！");
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString()); }
        }

        public static string SqliteoneQuery(string CommandText)
        {
            using (SQLiteConnection conn = new SQLiteConnection($"Data Source={AppDomain.CurrentDomain.BaseDirectory}ConfigInfo.db;Version=3"))
            {
                conn.Open();
                SQLiteCommand cmd = new SQLiteCommand();
                cmd.Connection = conn;
                cmd.CommandText = CommandText;
                return cmd.ExecuteScalar()?.ToString();
                //if (cmd.ExecuteScalar() != null) return cmd.ExecuteScalar().ToString();
                //else return string.Empty;
            }
        }

   
    }
}
